#!/bin/bash

TOOLS="$HOME/.dotnet/tools"
if [[ ":$PATH:" != *":$TOOLS:"* ]]; then
    export PATH="$PATH:$TOOLS"
fi

cnt=$(find StudentVideo.Data/Migrations -name '*updateDatabase?.cs' 2>/dev/null | wc -l)
((cnt++))

echo -e "\nCreating migration..."
migration="updateDatabase$cnt"
echo "Name: $migration"$'\n'

if ! dotnet ef migrations add "$migration" --project StudentVideo.Data/StudentVideo.Data.csproj --startup-project StudentVideo/StudentVideo.csproj 2>&1 | grep -v "^$"; then
    echo -e "\nError: Build failed. Running diagnostics..."$'\n'
    dotnet build StudentVideo/StudentVideo.csproj
    exit 1
fi

echo -e "\nOperation succeeded."
exit 0
